package com.sevennotes.classtable.ui.pages.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.MaterialTheme
import androidx.compose.material.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.OutlinedIconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.android.style.LineHeightStyleSpan
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlin.reflect.KProperty

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NumberInput(
  modifier: Modifier = Modifier,
  initialNumber: Int,
  onNumberChanged: (Int) -> Unit,
  onMinusClick: () -> Unit = {},
  onPlusClick: () -> Unit = {},
  textStyle: TextStyle = LocalTextStyle.current.copy(
    fontSize = 18.sp,
    textAlign = TextAlign.End,
  ),
  backgroundColor: Color = MaterialTheme.colors.surface,
  cornerRadius: Dp = 15.dp
) {
  Row(
    modifier = Modifier.border(width = 2.dp, color = Color.Black, shape = RoundedCornerShape(10.dp)),
    horizontalArrangement = Arrangement.Center,
    verticalAlignment = Alignment.CenterVertically
  ) {
    BasicTextField(
      modifier = Modifier.width(50.dp).padding(start = 3.dp),
      value = initialNumber.toString(),
      onValueChange = {
        onNumberChanged(try { it.toInt() } catch (e: Exception) { 0 })
      },
      keyboardOptions = KeyboardOptions.Default.copy(
        keyboardType = KeyboardType.Number
      ),
      textStyle = textStyle,
      singleLine = true,
    )
    Spacer(modifier = Modifier.width(10.dp))
    Column(
      modifier = Modifier.padding(top = 2.dp, bottom = 2.dp, end = 5.dp)
    ) {
      Icon(
        modifier = Modifier
          .size(20.dp)
          .clickable { onPlusClick() },
        imageVector = Icons.Filled.KeyboardArrowUp,
        contentDescription = "Plus"
      )
      Icon(
        modifier = Modifier
          .size(20.dp)
          .clickable { onMinusClick() },
        imageVector = Icons.Filled.KeyboardArrowDown,
        contentDescription = "Plus"
      )
    }
  }
}

@Preview(showBackground = true)
@Composable
fun InputBoxPreview() {
  NumberInput(initialNumber = 0, onNumberChanged = {})
}